<?php
namespace app\admin\model;

use think\Db;
use think\Model;
use think\Session;

class AdminNav extends Model
{
    /**
     * 获取全部菜单
     * @param  string $type tree获取树形结构 level获取层级结构
     * @return array       	结构数据
     */
    public function getTreeData($type='tree',$order=''){
        vendor('Org.Nx.Data#class');
        // 判断是否需要排序
        if(empty($order)){
            $data=Db::name('admin_nav')->select();
        }else{
            $data=Db::name('admin_nav')->order('order_number is null,'.$order)->select();
        }
        // 获取树形或者结构数据
        if($type=='tree'){
            $data=\Data::tree($data,'name','id','pid');
        }elseif($type="level"){
            $data=\Data::channelLevel($data,0,'&nbsp;','id');
            // 显示有权限的菜单
            $auth=new \think\Auth();
            foreach ($data as $k => $v) {
                if ($auth->check($v['mca'],Session::get('user')['id'])) {
                    foreach ($v['_data'] as $m => $n) {
                        if(!$auth->check($n['mca'],Session::get('user')['id'])){
                            unset($data[$k]['_data'][$m]);
                        }
                    }
                }else{
                    // 删除无权限的菜单
                    unset($data[$k]);
                }
            }
        }
//        print_r($data);
        return $data;
    }
}